define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {

    var Controller = {
        index: function () {
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'qingdongams/finance/invoice/index?type=0',
                    add_url: 'qingdongams/finance/invoice/add',
                    edit_url: 'qingdongams/finance/invoice/edit',
                    del_url: 'qingdongams/finance/invoice/del',
                    detail_url : 'qingdongams/finance/invoice/detail',
                    customer_url : 'qingdongams/customer/customer/detail',
                    contract_url:'qingdongams/customer/contract/detail',
                    table: 'invoice'
                }
            });

            $(document).on('click', '.show-customer', function (data) {
                var area = ['95%', '95%'];
                var options = {
                    shadeClose : false,
                    shade      : [0.3, '#393D49'],
                    area       : area,
                    callback   : function (value) {
                        //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                        console.log(value);
                    }
                };
                Fast.api.open($.fn.bootstrapTable.defaults.extend.customer_url + "?ids=" + $(this).data('id'), '客户详情', options);
            });
            $(document).on('click', '.show-contract', function (data) {
                var area = ['95%', '95%'];
                var options = {
                    shadeClose : false,
                    shade      : [0.3, '#393D49'],
                    area       : area,
                    callback   : function (value) {
                        //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                        console.log(value);
                    }
                };
                Fast.api.open($.fn.bootstrapTable.defaults.extend.contract_url + "?ids=" + $(this).data('id'), '客户详情', options);
            });

        },

        tableinfo:{
            url: '',
            pk: 'id',
            toolbar: '#toolbar',
            sortName: 'id',
            fixedRightNumber:1,
            fixedColumns:true,
            columns: [
                [
                    {field: 'state', checkbox: true},
                    {field: 'number', title: __('发票申请编号'), operate:false, formatter : function (value, row, index) {

                            return "<a href='javascript:void(0);' class='btn-dialog' data-area=[\"95%\",\"95%\"]  data-shade=\"0.3\"\n" +
                                "data-url=\""+ $.fn.bootstrapTable.defaults.extend.detail_url + "?ids=" + row.id+
                                "\" data-title=\"发票详情\" ' >" + value + "</a>";
                        }},

                    {field: 'customer', title: __('客户名称'), operate:false, formatter : function (value, row, index) {
                            if(row.customer){
                                return "<a href='javascript:void(0);' data-id='" + row.customer.id + "' class='show-customer'>" + row.customer.name + "</a>";
                            }else{
                                return '';
                            }
                        }},
                    {field: 'contract', title: __('合同编号'), operate:false, formatter : function (value, row, index) {
                            if(row.contract){
                                return "<a href='javascript:void(0);' data-id='" + row.contract.id + "' class='show-contract'>" + row.contract.num + "</a>";
                            }else{
                                return '';
                            }
                        }},

                    {field: 'money', title: __('开票金额')},
                    {field: 'time', title: __('开票日期'), operate: '=', addclass: 'datetimepicker',data:"data-date-format=\" YYYY-MM-DD\""},
                    {field: 'type', title: __('开票类型'), formatter: Table.api.formatter.status,  searchList: {0: __('增值税专用发票'), 1: __('增值税普通发票'), 2: __('国税通用机打发票'), 3: __('地税通用机打发票'), 4: __('收据')}},

                    {field: 'check_status', title: __('审批状态'), formatter: Table.api.formatter.status,  searchList: {0: __('待审核'), 1: __('审核中'), 2: __('审核通过'), 3: __('审核拒绝'), 4: __('撤销'), 5: __('草稿')}},
                    {field: 'owner_staff.name', title: __('创建人'),operate:false},
                    {field: 'invoice_number', title: __('发票号码')},
                    {field: 'invoice_time', title: __('实际开票时间'), operate: '=', addclass: 'datetimepicker',data:"data-date-format=\" YYYY-MM-DD\""},
                    {field: 'logistics', title: __('物流单号')},
                    {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                    {field: 'operate', title: __('Operate'),  events: Table.api.events.operate, formatter: Table.api.formatter.buttons,
                        buttons: [
                            {
                                name: 'detail',
                                text: __('标记为开票'),
                                title: __('标记为开票'),
                                extend:'data-area=["80%","80%"]',
                                classname: 'records btn-dialog',
                                url: 'qingdongams/finance/invoice/batchstatus',
                                visible: function (row) {
                                    if(row.invoice_time){
                                        return false;
                                    }
                                    return true;
                                },

                            },
                            {
                                name: 'detail',
                                text: __('已开票'),
                                title: __('已开票'),
                                extend:'data-area=["80%","80%"]',
                                classname: 'gray',
                                disabled:true,
                                visible: function (row) {
                                    //返回true时按钮显示,返回false隐藏
                                    if(row.invoice_time){
                                        return true;
                                    }
                                    return false;
                                },

                            },

                        ]
                    }
                ]
            ],
            //启用普通表单搜索
            search:false,
            commonSearch: true,
            searchFormVisible: true,
            onLoadSuccess:function(){
                // 这里就是数据渲染结束后的回调函数
                $(".btn-add").data("area",["90%","90%"]);
                $(".btn-edit").data("area",["90%","90%"]);
            }
        },
        table: {
            first: function () {
                var table = $("#table");

                Controller.tableinfo.url=location.href+'&type=0';
                Controller.tableinfo.toolbar='#toolbar';
                Controller.tableinfo.columns[0][Controller.tableinfo.columns[0].length-1].table=table;

                // 初始化表格
                table.bootstrapTable(Controller.tableinfo);

                // 为表格绑定事件
                Table.api.bindevent(table);
            },
            second: function () {
                // 初始化表格参数配置
                Table.api.init({});

                var table = $("#table1");

                Controller.tableinfo.url=location.href+'&type=1';
                Controller.tableinfo.toolbar='#toolbar1';
                Controller.tableinfo.columns[0][Controller.tableinfo.columns[0].length-1].table=table;

                // 初始化表格
                table.bootstrapTable(Controller.tableinfo);
                // 为表格绑定事件
                Table.api.bindevent(table);
            },
            third: function () {
                // 初始化表格参数配置
                Table.api.init({});

                var table = $("#table2");

                Controller.tableinfo.url=location.href+'&type=2';
                Controller.tableinfo.toolbar='#toolbar2';
                Controller.tableinfo.columns[0][Controller.tableinfo.columns[0].length-1].table=table;

                // 初始化表格
                table.bootstrapTable(Controller.tableinfo);
                // 为表格绑定事件
                Table.api.bindevent(table);
            },
        },
        add    : function () {

            Controller.api.bindevent();
        },
        batchstatus : function () {
            Controller.api.bindevent();
        },
        edit    : function () {
            Controller.api.bindevent();
        },
        detail : function () {
            $('.stafflist').popover({
                placement : 'bottom',
                trigger : 'hover'
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                var num = 0;

                $(document).on("click", ".select-customer", function(){
                    var $this=$(this);
                    top.Fast.api.open($(this).data("url") , __('Select'), {callback: function (data) {
                            $this.html(data.name);
                            $('[name="'+$this.data('name')+'"]').val(data.id);
                        },area:["100%", "100%"]});
                }).on("click", ".select-contract", function(){
                    var $this=$(this),customer_id=$('[name="row[customer_id]"]').val();
                    if(!customer_id){
                        Layer.msg('请先选择客户');
                        return false;
                    }
                    top.Fast.api.open($(this).data("url")+"&customer_id="+customer_id , __('Select'), {callback: function (data) {
                            $this.html(data.name);
                            $('#c-money').val(data.money);
                            $('[name="'+$this.data('name')+'"]').val(data.id);
                        },area:["100%", "100%"]});
                });

                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {}
        },

    };
    return Controller;
});